<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Reply'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                投诉回复
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >

                    <FormItem
                        label="回复"
                        type="textarea"
                        v-model="baTable.form.items!.mess"
                        prop="tel"
                        placeholder="输入回复内容"
                        v-if="baTable.form.items!.status != 1"
                    />
                    <FormItem :label="t('complaint.images')" type="images" v-model="baTable.form.items!.reply_images" prop="reply_images" v-if="baTable.form.items!.status != 1"/>
                    <FormItem
                        :label="t('complaint.status')"
                        type="radio"
                        v-model="baTable.form.items!.status"
                        prop="status"
                        :input-attr="{ content: { '0': '待处理', '1': '已完成', '2': '处理中' } }"
                        :placeholder="t('Please select field', { field: t('complaint.status') })"
                    />
                    
 
                </el-form>

                 <el-card style="margin-left: 10%;margin-bottom: 20px;" header="历史回复记录">
                             
                                <el-timeline style="max-width: 600px">
                                    <el-timeline-item :timestamp="item.timestamp" placement="top" v-for="(item,index) in record" :key="index">
                                    <el-card>
                                        <h4>我的回复：{{ item.mess }}</h4>
                                        <p v-if="item.imgs.length > 0">
                                            <el-image v-for="(item2,index2) in item.imgs" :src="'http://localhost:8000'+item2" :key="index2" class="pic" fit="cover" :preview-src-list="['http://localhost:8000'+item2]"/>
                                        </p>
                                    </el-card>
                                    </el-timeline-item>
                                    

                                    <el-timeline-item :timestamp="baTable.form.items!.timestamp" placement="top">
                                    <el-card>
                                        <h4>用户投诉：{{  baTable.form.items!.tel }}</h4>
                                        <p>投诉商铺：{{ baTable.form.items!.merchant.name }}</p>
                                        <p>投诉类型：重量问题</p>
                                        <p>投诉内容：{{ baTable.form.items!.content }}</p>
                                    </el-card>
                                    </el-timeline-item>
                                    
                                </el-timeline>
                       
                 </el-card>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { watch,inject, reactive, ref,onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import { getReply } from '/@/api/backend'
const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass
const mess  = ref("")
const { t } = useI18n()
const record = ref([])
const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    code: [buildValidatorData({ name: 'required', title: t('complaint.code') })],
    merchant_id: [buildValidatorData({ name: 'required', title: t('complaint.merchant_id') })],
    timestamp: [buildValidatorData({ name: 'date', title: t('complaint.timestamp') })],
})
watch(
    () => baTable.form.operate,
    (newVal) => {
         if(newVal === 'Reply'){
            getReply('get',{'id':baTable.form.items!.id})
                .then((res) => {
                    record.value = res.data.list
                })
                .finally(() => {
                    
                })
         }
    }
)
 
</script>

<style scoped lang="scss">
.pic{
    width: 100px;
    height: 100px;
    border-radius:5px;
    margin-top: 15px;
}
.recordbox{
    padding-left: 10%;
    border:1px solid #eee;
    border-radius: 7px;
    padding-top:20px;
}
</style>
